package com.use.userms2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MessageAdapter extends Activity implements View.OnClickListener {
    private TextView etListInfo;



    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        etListInfo = findViewById(R.id.tvListInfo);

        findViewById(R.id.btRefresh).setOnClickListener(this);
        findViewById(R.id.btnMsg).setOnClickListener(this);
        findViewById(R.id.btnAdd).setOnClickListener(this);
        //创建数据库及表
        db = openOrCreateDatabase("TestDB", Context.MODE_PRIVATE, null);
        updateUserView();

    }


    public void updateUserView() {
        Cursor cur = db.rawQuery("select * from user", null);
        int sum = cur.getCount();
        String sUser = String.format("共有记录数量：%d \n", sum);
        for (int i = sum - 1; i > 1; i--) {
            cur.moveToPosition(i);
            sUser += String.format("%s,%s,%s\n", cur.getString(0), cur.getString(1), cur.getString(2));
        }
        etListInfo.setText(sUser);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btnMsg){
            System.out.println("Listactivity.java run  btnMsg ------------ok");
            Intent intentMsg = new Intent();

            startActivity(intentMsg);
        }
        if (view.getId() == R.id.btRefresh) {
            System.out.println("Listactivity.java run btRefresh ------------ok");
            // updateUserView();
            Intent intentMsg = new Intent();

            startActivity(intentMsg);
        }
        if (view.getId() == R.id.btnAdd) {
            System.out.println("Listactivity.java run btnAdd ------------ok");

        }
        }
    }